import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:zolon_printer/zolon_printer.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _zolonPrinterPlugin = ZolonPrinter();

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion =
          await _zolonPrinterPlugin.getPlatformVersion() ?? 'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              Text('Running on: $_platformVersion\n'),
              TextButton(onPressed: () async {
                await _zolonPrinterPlugin.configM9200Printer(0, 40, 30);
              }, child: const Text('configM9200Printer', style: TextStyle(color: Colors.black, fontSize: 16),)),
              TextButton(onPressed: () async {
                await _zolonPrinterPlugin.m9200PrintLabel("text", 40, 30);
              }, child: const Text('m9200PrintLabel', style: TextStyle(color: Colors.black, fontSize: 16),)),
              TextButton(onPressed: () async {
                await _zolonPrinterPlugin.m9200PrintTicket('{"id":111675,"shopId":65,"tableName":"2","areaName":"大厅","tableId":140,"eatNumber":2,"seqNumber":"大厅 2","eatWay":1,"prePay":false,"otherPackagePrice":0,"otherPrice":0,"otherPriceDiscountPrice":null,"no":"DD2501181945233848","createTime":1737200723000,"userRemark":null,"status":21,"productCount":null,"finishTime":null,"cancelTime":null,"commentStatus":false,"payStatus":false,"payOrderId":111742,"payTime":null,"payExpireTime":1737269326854,"payChannelCode":null,"payChannelName":null,"totalPrice":6800,"sumPrice":6800,"discountPrice":0,"actualDiscount":0,"deliveryPrice":0,"adjustPrice":0,"payPrice":6800,"deliveryType":null,"logisticsNo":null,"deliveryTime":null,"receiveTime":null,"settlementName":"店主","settlementTime":null,"receiverName":"店主","receiverMobile":"13880342484","receiverAreaId":null,"receiverAreaName":null,"receiverDetailAddress":null,"pickUpStoreId":null,"couponId":null,"couponPrice":0,"pointPrice":0,"itemBatchs":[{"times":1,"userName":"店主","createTime":1737200723000,"eatWay":1,"count":4.0,"totalPrice":6800,"items":[{"id":543290,"discountExtraPrice":0,"spuId":198,"spuName":"重庆鬼血旺","unit":"","skuId":1034,"skuName":"","keyValue":"","remark":null,"properties":null,"picUrl":"https://wx-css.erperp.cn/jiuge/65/uvXxFnZbRlgdhHE5SSS9.png","count":1.0,"times":1,"commentStatus":false,"price":3200,"discountPrice":0,"payPrice":0,"payPriceAll":null,"payPriceExtra":null,"discountPriceTotal":0,"rateInTotalPrice":null,"afterSaleStatus":0},{"id":543291,"discountExtraPrice":0,"spuId":200,"spuName":"双江头刀菜","unit":"","skuId":1036,"skuName":"","keyValue":"","remark":null,"properties":null,"picUrl":"https://wx-css.erperp.cn/jiuge/65/uAMQXYjrW8Pt1hgYIL5c.png","count":1.0,"times":1,"commentStatus":false,"price":3200,"discountPrice":0,"payPrice":0,"payPriceAll":null,"payPriceExtra":null,"discountPriceTotal":0,"rateInTotalPrice":null,"afterSaleStatus":0},{"id":543292,"discountExtraPrice":0,"spuId":250,"spuName":"米饭（客）","unit":"客","skuId":1086,"skuName":"","keyValue":"","remark":null,"properties":null,"picUrl":"https://wx-css.erperp.cn/jiuge/65/u2U4CHWwDlSDfNggeWmu.png","count":2.0,"times":1,"commentStatus":false,"price":200,"discountPrice":0,"payPrice":0,"payPriceAll":null,"payPriceExtra":null,"discountPriceTotal":0,"rateInTotalPrice":null,"afterSaleStatus":0}]}],"eatWayName":"堂食"}', '{"id":65,"createTime":1708507719000,"expireDateTime":1816617599999,"loginDate":1737119404000,"description":null,"eatWayPackage":true,"eatWayRoom":true,"freePackage":true,"mobile":null,"name":"小蝉中餐厅","openStatus":true,"openTime":["00:00-24:00"],"openTimeStr":null,"orderCount":null,"payAfter":true,"payBefore":true,"payWay":2,"picUrl":"https://wx-css.erperp.cn/jiuge/0/ubJfTKtoisXAww5Y76cc.png","qrCodeUrl":"https://wx-css.erperp.cn/jiuge/65/qrcode/shop/3a768755-5b35-4965-b59d-44a4720e4bd2.png","qrCodeUrlSimple":"https://wx-css.erperp.cn/jiuge/65/qrcode/shop/ce31b034-0dd9-4be9-aa39-c93fbe103ca6_simple.png","shopCode":null,"shopDesc":"欢迎光临本店，请扫码二维码开始点单吧！本店啤酒自10月1日起到10月31日为止，每消费两瓶赠送一瓶，赠送不限量！","sort":0,"spuCount":null,"lakalaConfig":{"open":false,"fullName":"小蝉中餐厅","simpleName":"小蝉中餐厅","merchantNo":"822651058123UXA","termNo":"K4606888"},"scanConf":{"address":"校园路1号溪地湾","backgroudUrl":"https://wx-css.erperp.cn/jiuge/65/uQ7wmRjYYHVDUv86xKWv.png","contractPerson":"聂浪","contractPhone":"13880342484","licenseUrl":"https://wx-css.erperp.cn/2cd3d7bed5b96f8217f7a59559789fc4244a6c20defe10621e2b8f4469654995.png","locationConf":{"shen":"四川省","shi":"成都市","qu":"郫都区","xian":null}},"spuMustConf":{"spuWay":1,"spuIdList":[]},"status":1}', "settlement");
              }, child: const Text('m9200PrintTicket', style: TextStyle(color: Colors.black, fontSize: 16),))
            ],
          )
        ),
      ),
    );
  }
}
